package com.wdh.monitor.base;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.wdh.monitor.dto.MonitorDto;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;

@Slf4j(topic = "disruptor.mqUtil")
public class MonitorProducer {

    private Disruptor<MonitorDto> disruptor = new Disruptor<>(new MonitorFactory(), 1024, Executors.defaultThreadFactory());;

    private RingBuffer<MonitorDto> ringBuffer = null;

    public MonitorProducer(MonitorHandler monitorHandler){
        disruptor.handleEventsWith(monitorHandler);
        ringBuffer = disruptor.start();
    }

    public void sendMq(MonitorDto source) {
        long sequence = ringBuffer.next();
        try {
            MonitorDto target = ringBuffer.get(sequence);
            target.setStartTime(source.getStartTime());
            target.setEndTime(source.getEndTime());
            target.setResponseTime(source.getResponseTime());
            target.setExp(source.isExp());
            target.setMonitorName(source.getMonitorName());
            target.setAppName(source.getAppName());
        } finally {
            ringBuffer.publish(sequence);
        }
    }
}
